package charlie.shop.admin;

import charlie.shop.misc.Common;
import com.charlie.beans.Groups;
import com.charlie.db.DAOLocal;
import com.charlie.sessions.GroupsFacadeLocal;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *Show all groups servlet
 * 
 * @author Оленка
 */
@WebServlet(name = "AllGroups", urlPatterns = {"/admin/groups"})
public class AllGroups extends HttpServlet {

    /**
     * Items per page limit static constant
     */
    private static final int ITEMS_PER_PAGE = 20;
    @EJB
    private GroupsFacadeLocal groupsFacade;
    @EJB
    private DAOLocal dao;

    /**
     * Return pages information and users data
     * 
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");       
    
        int page = Common.getPage(request);
        int itemsCount = groupsFacade.count();
        int pagesCount = (int) Math.ceil((float) itemsCount / (float) ITEMS_PER_PAGE);

        List<Groups> records = null;
        List<String> groupRoles = new ArrayList<String>();

        if (page <= pagesCount) {
            records = dao.getGroupsRange(((page - 1) * ITEMS_PER_PAGE), (page * ITEMS_PER_PAGE));
            for (Groups g : records) {
                String s = dao.getGroupRolesNames(g.getGroupsId());
                if (s == null) {
                    groupRoles.add("");
                } else {
                    groupRoles.add(s);
                }
            }
        }

        request.setAttribute("page", page);
        request.setAttribute("pagesCount", pagesCount);

        request.setAttribute("groupsList", records);
        request.setAttribute("groupRoles", groupRoles);
        response.setContentType("text/html;charset=UTF-8");
        getServletContext().getRequestDispatcher("/admin/allGroups.jsp").forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        processRequest(request, response);
    }
}
